package com.example.swd1.gudong.main.message;

import android.content.Context;
import android.text.TextPaint;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


import com.example.swd1.gudong.R;
import com.example.swd1.gudong.model.MessageItem;

import java.util.List;


//==================================================================================================================
//   Author :  Shile Wu
//   Date  :  16-8-19
//   Role :    xx
//   Reference documents : xx
//==================================================================================================================
public class MessageListViewAdapter extends BaseAdapter{
    private Context context;
    private List<MessageItem> list;

    public MessageListViewAdapter(Context context, List<MessageItem> list){
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        viewHolder holder;
        if(convertView == null){
            holder = new viewHolder();
            convertView = View.inflate(context, R.layout.item_message_list,null);
            holder.messageTitle = (TextView) convertView.findViewById(R.id.message_title);
            TextPaint tp = holder.messageTitle.getPaint();
            tp.setFakeBoldText(true);
            holder.messageContent = (TextView) convertView.findViewById(R.id.message_content);
            holder.messageTime = (TextView) convertView.findViewById(R.id.message_time);
            holder.itemUnReadPoint = (ImageView) convertView.findViewById(R.id.item_unread_point);
            holder.itemUnReadNum = (TextView) convertView.findViewById(R.id.item_unread_num);
            convertView.setTag(holder);

        } else {
            holder = (viewHolder) convertView.getTag();
        }
        holder.messageTitle.setText(list.get(position).getMessageTitle());
        holder.messageContent.setText(list.get(position).getMessageContent());
        holder.messageTime.setText(list.get(position).getMessageTime());
        if(list.get(position).getUnReadNum() > 0){
            holder.itemUnReadPoint.setVisibility(View.VISIBLE);
            holder.itemUnReadNum.setVisibility(View.VISIBLE);
            holder.itemUnReadNum.setText(list.get(position).getUnReadNum()+"");
        }else {
            holder.itemUnReadPoint.setVisibility(View.GONE);
            holder.itemUnReadNum.setVisibility(View.GONE);
        }
        return convertView;
    }

    private class viewHolder{
        ImageView avatar;
        TextView messageTitle;
        TextView messageContent;
        TextView messageTime;
        ImageView itemUnReadPoint;
        TextView itemUnReadNum;
    }
}
